package com.example.maple.ccsclient.app;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;

import com.example.maple.ccsclient.R;
import com.example.maple.ccsclient.adapter.AlbumsRecyclerViewAdapter;
import com.example.maple.ccsclient.adapter.RecyclerViewAdapter;
import com.example.maple.ccsclient.instance.DataInfoInstance;
import com.example.maple.ccsclient.model.PicModel;
import com.example.maple.ccsclient.presenter.HomePagePresenter;
import com.example.maple.ccsclient.view.OnItemClickListener;
import com.example.maple.ccsclient.view.SpaceItemDecoration;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class PicChoiceActivity extends Activity {

	List<String> mList;

	Button cancel;
	Button sure;
	RecyclerView mRecyclerView;
	AlbumsRecyclerViewAdapter mAdapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_pic_choice);

		findView();
	}

	private void findView() {
		cancel = (Button) findViewById(R.id.back);
		sure = (Button) findViewById(R.id.refresh);
		mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
		mRecyclerView.setLayoutManager(new GridLayoutManager(this, 3));

		mList = new ArrayList<>();
		//间距
		mRecyclerView.setItemAnimator(new DefaultItemAnimator());
		mRecyclerView.addItemDecoration(new SpaceItemDecoration(this, 3));
		mAdapter = new AlbumsRecyclerViewAdapter(PicChoiceActivity.this, R.layout.album_checkbox_item, mList);
		mRecyclerView.setAdapter(mAdapter);
		mAdapter.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(View view, int position) {

			}
		});

		new MyAsyncTask().execute();
	}

	public void showFamousVersePic() {

		mAdapter.notifyDataSetChanged();

	}

	public static void startAction(Context context) {
		Intent  intent= new Intent(context,PicChoiceActivity.class);
		context.startActivity(intent);
	}

	public class MyAsyncTask extends AsyncTask<Nullable, Integer, String> {

		@Override
		protected String doInBackground(Nullable... params) {


			StringBuilder sb = new StringBuilder();
			File folder = new File("/sdcard/集字系统/");
			if (!folder.exists() && !folder.isDirectory()) {
				return "无集字图片请先保存图片";
			} else if (folder.isDirectory()) {
				String[] fileList = folder.list();
				if (fileList.length == 0) {
					return "无集字图片请先保存图片";
				}

				for (int i = 0; i < fileList.length; i++) {
					sb.append("file:///sdcard/集字系统/");
					sb.append(fileList[i]);
					mList.add(sb.toString());
					sb.delete(0, sb.length());

				}


				return "完成";
			}

			return null;
		}

		@Override
		protected void onProgressUpdate(Integer... values) {
			super.onProgressUpdate(values);

		}

		@Override
		protected void onPostExecute(String s) {
			super.onPostExecute(s);
			if (s.equals("完成")) {
				showFamousVersePic();
			}
		}
	}

}
